//
//  YZFriendApplyCell.m
//  VET
//
//  Created by lixiaoyi on 2018/7/13.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZFriendApplyCell.h"
#import "ChatHeader.h"

@implementation YZFriendApplyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createViews];
        [self layoutViews];
    }
    
    return self;
}


- (void)createViews {
    [self.contentView addSubview:self.textField];
    [self.textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        
        NSUInteger len = x.length;
        
        if (len <= 10) {
            
            [self.rightLabel setText:[NSString stringWithFormat:@"%ld/10",len]];
            self.inputStr = [self.textField.text substringToIndex:len];
            NSLog(@"self.inputStr------>%@",self.inputStr);
            
            if (self.inputBlock) {
                self.inputBlock(self.inputStr);
            }
            
        } else {
            
            [self.textField setText:self.inputStr];
        }
        
    }];
    
    [self.contentView addSubview:self.rightLabel];
    
}

#pragma mark - lazy load
- (UITextField *)textField {
    if (!_textField) {
        CGRect tfFrame = CGRectMake(10, 2, kWidth - 20, 40);
        _textField = [[UITextField alloc] initWithFrame:tfFrame];
        _textField.textAlignment = NSTextAlignmentLeft;
        _textField.borderStyle = UITextBorderStyleNone;
        _textField.clearButtonMode = UITextFieldViewModeAlways;
        _textField.placeholder = @"请输入验证申请";
        _textField.returnKeyType = UIReturnKeyDone;
        _textField.delegate = self;
    }
    
    return _textField;
}

- (UILabel *)rightLabel {
    if (!_rightLabel) {
        CGRect lblFrame = CGRectMake(0, 7, 40, 30);
        _rightLabel = [[UILabel alloc] initWithFrame:lblFrame];
        _rightLabel.textAlignment= NSTextAlignmentRight;
        _rightLabel.textColor = XZRGB(0x9B9B9B);
        _rightLabel.font = [UIFont systemFontOfSize:16];
        _rightLabel.text = @"0/10";
    }
    
    return _rightLabel;
}

- (void)layoutViews {
    
    UIView *superView = self.contentView;
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(superView.mas_left).with.offset(20);
        make.right.mas_equalTo(self.rightLabel.mas_left).with.offset(-20);
        make.top.mas_equalTo(superView.mas_top).with.offset(2);
        make.bottom.mas_equalTo(superView.mas_bottom).offset(-2);
    }];
    
    
    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.textField.mas_right).with.offset(20);
        make.right.mas_equalTo(superView.mas_right).with.offset(-20);
        make.top.mas_equalTo(superView.mas_top).with.offset(2);
        make.bottom.mas_equalTo(superView.mas_bottom).offset(-2);
        make.width.mas_equalTo(40);
    }];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    self.inputStr = @"";
    self.textField.text = @"";
    
    return YES;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
